package com.kuyun.parseexcel.parsemodule.strategyInterface;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.analysis.ExcelReadExecutor;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.kuyun.parseexcel.parsemodule.bean.TemplateBean;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public abstract class AbstractExcelDataHandle<T> extends AnalysisEventListener<T> implements ExcelDataHandle<T> {

    public AbstractExcelDataHandle(String file,Class clazz) {
        this.file = file;
        this.clazz = clazz;
    }

    private List<TemplateBean> templateList = new ArrayList<>();
    private String file;
    private Class clazz;

    private T data;

    public T getData() {
        return this.data;
    }

    public void setData(T t) {
        this.data = t;
    }

    public abstract void shareData();

    @Override
    public void invoke(T data, AnalysisContext context) {
        this.setData(data);
        this.shareData();
        if (this.invalidDataCondition()) {
            return;
        }
        TemplateBean build = TemplateBean
                .builder()
                .parkingNo(this.handleParkingNo())
                .authType(this.handleAuthType())
                .authNo(this.handleAuthNo())
                .deviceNo(this.handleDeviceNo())
                .deviceName(this.handleDeviceName())
                .entryNo(this.handleEntryNo())
                .entryName(this.handleEntryName())
                .carPlateColor(this.handleCarPlateColor())
                .carType(this.handleCarType())
                .parkingType(this.handleParkingType())
                .licensePlate(this.handleLicensePlate())
                .outOrIn(this.handleOutOrIn())
                .trafficTime(this.handleTrafficTime())
                .trafficType(this.handleTrafficType())
                .hasCapture(this.handleHasCapture())
                .other(this.handleOther())
                .appCode(this.handleAppCode())
                .build();
        templateList.add(build);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {


    }

    public void doExcelReaderBuilder() {
        ExcelReaderBuilder readerBuilder = EasyExcel.read(file, this);
        readerBuilder.head(clazz);
        ExcelReader excelReader = readerBuilder.build();
        ExcelReadExecutor excelReadExecutor = excelReader.excelExecutor();
        List<ReadSheet> readSheets = excelReadExecutor.sheetList();
        int sheetSize = readSheets.size();
        for (int i = 0; i < sheetSize; i++) {
            excelReader.read(readSheets.get(i));

            if (sheetSize == (i + 1)) {
                File file = new File(this.file);
                String[] split = file.getName().split("\\.");
                String fileName = "./temp/" + split[0] + ".xlsx";
                File file1 = new File(fileName);
                if (!file1.getParentFile().exists()) {
                    file1.getParentFile().mkdirs();
                }
                if (!file1.exists()) {
                    try {
                        file1.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                EasyExcel.write(file1.getAbsoluteFile(), TemplateBean.class).sheet("sheet").doWrite(this.templateList);
            }
        }

        excelReader.finish();

    }

}
